package com.example.administrator.androidwork5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String jsonStr = " {\"temp\":\"20℃/30℃\",\"weather\":\"晴转多云\",\"name\":\"上海\",\"pm\":\"80\",\"wind\":\"1级\"}";
        String jsonArrayStr = "[\n" +
                "  {\"temp\":\"20℃/30℃\",\"weather\":\"晴转多云\",\"name\":\"上海\",\"pm\":\"80\",\"wind\":\"1级\"},\n" +
                "  {\"temp\":\"15℃/24℃\",\"weather\":\"晴\",\"name\":\"北京\",\"pm\":\"98\",\"wind\":\"3级\"},\n" +
                "  {\"temp\":\"26℃/32℃\",\"weather\":\"多云\",\"name\":\"广州\",\"pm\":\"30\",\"wind\":\"2级\"}\n" +
                "]";
        try{
            JSONObject jsonObject = new JSONObject(jsonStr);
            String temp =jsonObject.getString("temp");
            String weather = jsonObject.getString("weayher1");
            Log.e(TAG, "onCreate: "+temp );

            JSONArray jsonArray = new JSONArray(jsonArrayStr);

            Log.e(TAG, "onCreate: "+jsonArray.length() );

            for (int i=0;i<jsonArray.length();i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                temp = jsonObject.getString("temp");
                weather = jsonObject1.getString("weather");
                Log.e(TAG, "onCreate: " + temp + "==" + weather);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
